package com.woniuxy.util;


import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * Servlet工具类
 *
 * @author 周川雄
 * @date 2022/1/3 20:38
 */
@SuppressWarnings("all")
public class ServletApiUtil {

    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
    }

    public static HttpServletResponse getResponse() {
        return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
    }

    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    public static void setSession(String key, Object value) {
        getSession().setAttribute(key, value);
    }

    public static <T> T getSessionValue(String key, Class<T> type) {
        return (T)(getSession().getAttribute(key));
    }

    public static void setRequestValue(String key, Object value) {
        getRequest().setAttribute(key, value);
    }

    public static <T> T getRequestValue(String key, Class<T> type) {
        return (T)(getRequest().getAttribute(key));
    }
}
